package bma.common.netty.protobuf.core.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bma.common.langutil.ai.stack.AIStack;
import bma.common.netty.protobuf.core.PDispatcher;
import bma.common.netty.protobuf.core.PRequestContext;
import bma.common.netty.protobuf.core.PService;

public class PDispatcherSimple implements PDispatcher {

	protected Map<String, PService> servicesMap = new HashMap<String, PService>();

	public void setServiceMap(Map<String, PService> map) {
		servicesMap.putAll(map);
	}

	public void setServices(List<PServiceBase> services) {
		for (PServiceBase s : services) {
			s.createMethods();
			servicesMap.put(s.getTarget(), s);
		}
	}

	public PService getSedrvice(String target) {
		return servicesMap.get(target);
	}

	public PService setService(PServiceBase s) {
		return servicesMap.put(s.getTarget(), s);
	}

	@Override
	public boolean dispatch(AIStack<PService> stack, PRequestContext ctx) {
		String target = ctx.getRequest().getTarget();
		return stack.success(servicesMap.get(target));
	}

}
